<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>

	<script type="text/javascript" src="resources/lib/jquery-3.4.1.js"></script>

</head>
<body>

	<h2>Login Page</h2>

	<span id="errorMessage" style="font-size: 20px;color: red;"></span>

	<form method="post" action="">
		UserName:<label><input name="userName" type="text"/></label>

		<br><br>

		PassWord:<label><input name="password" type="password"/></label>

		<br><br>
		验证码:<label><input name="kaptcha" type="text"/></label>

		<br><br>
		<img id="kaptcha" src="sys/user/captcha.do" onclick="refreshImage()" style="cursor: pointer;width: 100px" alt="验证码">

		<input type="button" value="Submit" onclick="formSub()"/>
	</form>

<script type="text/javascript">
	$(function () {

	});

	function formSub1() {
		var subDate = {
			"userName" : $("input[name=userName]").val(),
			"password" : $("input[name = password]").val(),
			"kaptcha" : $("input[name = kaptcha]").val()
		};
		$.post("${pageContext.servletContext.contextPath}/sys/user/login.do",subDate,function (result) {
			console.log(result);
			if (result.statusCode === "0000") {
				window.location.href = "${pageContext.servletContext.contextPath}/admin.jsp";
			} else {
				$("#errorMessage").text(result.statusDesc);
			}
		},"json")
	}

	function formSub() {
		var subDate = {
			"userName" : $("input[name=userName]").val(),
			"password" : $("input[name = password]").val(),
			"kaptcha" : $("input[name = kaptcha]").val()
		};
		$.ajax({
			url : "${pageContext.servletContext.contextPath}/sys/user/login.do",
			type : "POST",
			dataType : "json",
            contentType : "application/json",
			data: JSON.stringify(subDate),
			success : function (result) {
				if (result.statusCode === "0000") {
					window.location.href = "${pageContext.servletContext.contextPath}/admin.jsp";
				} else {
					$("#errorMessage").text(result.statusDesc);
				}
			},
			error : function (result) {
				alert("发生错误 : " + result.status);
			}
		})
	}

	function refreshImage() {
		$.get("sys/user/captcha.do",{},function (result) {
			$("#kaptcha").attr("src","sys/user/captcha.do");
		});
	}
</script>
</body>
</html>